package Model;
/*
 * Written by Phat Le and Dustin Stripplin
 */
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.swing.JTable;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import FileParser.FileParser;
import TableModels.MasterTable;
/**
 * Program Chair can designate SubProgram chair, make approval,
 * view manuscripts and reviews.
 * @author Phat and Dustin
 *
 */
public class ProgramChair extends User {

	private String name;
	private Conference conference;
	private String email;
	private Integer phone;
	private SubProgramChair sub[];
	private FileParser file_Parser;
	private ArrayList<Manuscript> manuscripts;
	
	/**
	 * 
	 */
	public ProgramChair(){
		
	}
	
	/**
	 * @param a_fileParser
	 * @param a_name
	 * @param a_conference
	 */
	public ProgramChair(FileParser a_fileParser, String a_name, Conference a_conference) {
		super(a_fileParser, a_name, a_conference);
		conference = a_conference;
		name = a_name;
		file_Parser = a_fileParser;
		manuscripts = a_fileParser.getManuscripts(Roles.PC.toString(), a_name, a_conference.getName());
	}
	
	
	/**
	 * @return
	 */
	public SubProgramChair[] getSub() {
		return sub;
	}

	/**
	 * @param sub
	 */
	public void setSub(SubProgramChair[] sub) {
		this.sub = sub;
	}
	
	/**
	 * @return
	 */
	public ArrayList<Manuscript> getManuscripts() {
		
		 return manuscripts; 
	}
	

	/**
	 * @param table
	 * @return
	 */
	public String viewManuscript(final JTable table) {
		String manuscript = "";
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
						
			String link = ((MasterTable)table.getModel()).getManuscriptLink(row);//get link	
			InputStream stream = FileParser.class.getResourceAsStream(link);	//convert to stream
			try { 
					XWPFDocument docx = new XWPFDocument(stream);				//parse docx
					XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
					manuscript = we.getText();				
			
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
					stream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		return manuscript;
	}
  
	public void makeApproval(String a_title, int a_aprVal) {
		file_Parser.makeApproval(a_title, a_aprVal);
	}
	
	/**
	 * make changes to approval of  paper and save to XML file via parser.
	 * @param a_manuscript paper to be modified
	 * @param a_title new title
	 * @param a_link new link
	 * @return the changed manuscript, null if error occurred
	 * @author SAMBA,Patrick (SAVE MANUSCRIPT METHOD)
	 */
	public Manuscript modifyPaper(final Manuscript a_manuscript, final int a_apprValue) {
		Manuscript m = new Manuscript(a_manuscript);
		m.setApproval(a_apprValue);
		
		file_Parser.saveManuscript(a_manuscript.getTitle(), m);
		manuscripts = file_Parser.getManuscripts(Roles.PC.toString(), name, conference.getName());
		
		return m;
	}

	/**
	 * @param a_spc
	 * @param a_manuscript
	 */
	public void designateSubprogramChair(final String a_spc, final Manuscript a_manuscript) {
		file_Parser.designateSubProgramChair(a_spc, a_manuscript);
	}

	/**
	 * @param table
	 * @return
	 */
	public ArrayList<String> viewReview(final JTable table) {
		ArrayList<String> review = new ArrayList<String>();
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
							//must cast to correct TableModel, or getManuscriptLink wont work.

				
			//String links = ((TemplateTableModel)table.getModel()).getManuscriptLink(row);//get link	
			ArrayList<Review> links = ((MasterTable)table.getModel()).getReviews(row);//get link
			if(!links.isEmpty()) {
				for(int i = 0; i < links.size(); i++) {
					InputStream stream = FileParser.class.getResourceAsStream(links.get(i).getReviewLink());	//convert to stream
					try { // apache poi documentation  http://poi.apache.org/apidocs/index.html
							XWPFDocument docx = new XWPFDocument(stream);				//parse docx
							XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
							review.add(i, we.getText());						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
							stream.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		}
 		return review;
	}
	
	/**
	 * @param a_name
	 * @return
	 */
	public TreeSet<String> getUserList(final String a_name){
		return file_Parser.getUserList(a_name);
	}
	
	/**
	 * @return the last manuscript that was submitted
	 */
	public Manuscript getLatestManuscript() {
		return manuscripts.get(manuscripts.size()-1);
	}
	
	
}
